/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmProduct.java
 *
 * Created on May 22, 2011, 8:06:44 AM
 */

package Ordermanagement;

import Connections.DataTools;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author BachDEV
 */
public class frmProduct extends javax.swing.JFrame {

     JTable tblProduct = null;
     Vector vecPro = new Vector();
     frmOrder order = new frmOrder(null);

    /** Creates new form frmProduct */
    public frmProduct(frmOrder order) {

        try{
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }catch(InstantiationException ex){
            ex.printStackTrace();
        }catch(UnsupportedLookAndFeelException ex){
            ex.printStackTrace();
        }catch(ClassNotFoundException ex){
            ex.printStackTrace();
        }catch(IllegalAccessException ex){
            ex.printStackTrace();
        }
        initComponents();
        try
        {
            this.order = order;
            DataTools dt = new DataTools();
            showProduct(dt.getProduct());
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }
    //////////////////////////////////////////////////////////////////////////
    public void showProduct(Vector vec)
    {
        try
        {
            btnAdd.setEnabled(false);
            Vector ve = new Vector();
            ve.add("ID");
            ve.add("Name");
            ve.add("CateID");
            ve.add("Price");
            ve.add("Month warranty");
            ve.add("Description");
            ve.add("Status");
            tblProduct = new JTable(vec, ve);
            scrProduct.setViewportView(tblProduct);
            tblProduct.getColumnModel().getColumn(0).setMinWidth(0);
            tblProduct.getColumnModel().getColumn(0).setMaxWidth(0);
            tblProduct.getColumnModel().getColumn(2).setMinWidth(0);
            tblProduct.getColumnModel().getColumn(2).setMaxWidth(0);
            tblProduct.addMouseListener(new MouseListener() {

                public void mouseClicked(MouseEvent e) {
                     btnAdd.setEnabled(true);
                     int row = tblProduct.getSelectedRow();
                     for(int i = 0; i < 7; i++)
                     {
                        vecPro.add(tblProduct.getValueAt(row, i));
                     }
                     for(int i = 0; i < 7; i++)
                     {
                         System.out.println("pRO: " +vecPro.get(i));
                     }
                }

                public void mousePressed(MouseEvent e) {

                }

                public void mouseReleased(MouseEvent e) {

                }

                public void mouseEntered(MouseEvent e) {

                }

                public void mouseExited(MouseEvent e) {

                }
            });
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        scrProduct = new javax.swing.JScrollPane();
        btnAdd = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Product");

        btnAdd.setText("Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });

        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(scrProduct, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 638, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(258, 258, 258)
                .addComponent(btnAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(41, 41, 41)
                .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(197, 197, 197))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(scrProduct, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnExit)
                    .addComponent(btnAdd))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        order.txtProName.setText(vecPro.get(1).toString());
        order.proID = Integer.parseInt(vecPro.get(0).toString());
        this.dispose();
}//GEN-LAST:event_btnAddActionPerformed

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        dispose();
        order.setAlwaysOnTop(true);
}//GEN-LAST:event_btnExitActionPerformed

    /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new frmProduct(null).setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnExit;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane scrProduct;
    // End of variables declaration//GEN-END:variables

}
